//
//  WCNForgotPwdVC.m
//  WCN
//
//  Created by 杨鑫 on 15/5/30.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNForgotPwdVC.h"
//
#import "NSString+Utilities.h"
//
#import "WCTimer.h"
//
#import "WCPopupLayerManager.h"
#import "WCNetworkAccessManager.h"
//
#import "Constants.h"

static __weak WCTimer *timerFgt;

@interface WCNForgotPwdVC () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation WCNForgotPwdVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.title = @"忘记密码";
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    timerFgt.securityCode = self.codeBtn;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 倒计时

- (void)countDown
{
    __block WCTimer *timer = [[WCTimer alloc] init];
    timer.count = 60;
    timer.securityCode = self.codeBtn;
    timer.timeEnd = ^{
        timer = nil;
    };
    [timer start:1];
    
    timerFgt = timer;
}

#pragma mark - Notification

- (void)keyboardWillHide:(NSNotification *)note
{
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformIdentity;
                     } completion:NULL];
}

#pragma mark - Nib Target

//发送验证码
- (IBAction)codeBtnTap:(id)sender {
    NSString *mobile = self.phoneTextField.text;
    if ([mobile isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"手机号不能为空" withView:self.view];
        return;
    }
    if ([mobile isMobile]) {
        self.codeBtn.enabled = NO;
        
        MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
        [[WCNetworkAccessManager sharedMg] POST:[NSString stringWithFormat:@"nurse/account/resetpwd/verifyCode/%@", mobile]
                                     parameters:nil
                                        success:^(NSDictionary *data){
                                            [[WCPopupLayerManager shareMg] showTextOnly:data[@"data"] withView:self.view];
                                            
                                            //倒计时
                                            [self countDown];
                                        } failure:^(NSString *msg){
                                            self.codeBtn.enabled = YES;
                                            [[WCPopupLayerManager shareMg] showTextOnly:msg withView:[UIApplication sharedApplication].keyWindow];
                                        } netError:^(id error){
                                            self.codeBtn.enabled = YES;
                                        } always:^{
                                            [hud hide:YES];
                                        }];
    } else {
        [[WCPopupLayerManager shareMg] showTextOnly:@"手机号错误" withView:self.view];
        return;
    }
}

- (IBAction)bgTouchDown:(id)sender {
    [self.view endEditing:YES];
}

- (IBAction)togglePwdBtn:(id)sender {
    UIButton *btn = sender;
    
    if (btn.selected) {
        [btn setSelected:NO];
        
        self.pwdTextField.secureTextEntry = YES;
    } else {
        [btn setSelected:YES];
        
        NSString *passwordStr = self.pwdTextField.text;
        self.pwdTextField.text = @"";
        self.pwdTextField.secureTextEntry = NO;
        self.pwdTextField.text = passwordStr;
    }
}

- (IBAction)doSubmit:(id)sender {
    NSString *account = self.phoneTextField.text, *code = self.codeTextField.text, *password = self.pwdTextField.text;
    
    if ([account isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请输入手机号" withView:self.view];
        return;
    }
    
    if ([code isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请输入验证码" withView:self.view];
        return;
    }
    
    if ([password isEmpty]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"请输入密码" withView:self.view];
        return;
    }
    
    if ([password hasSpace]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"密码不能有空格" withView:self.view];
        return;
    }
    
    if (![password isPassword]) {
        [[WCPopupLayerManager shareMg] showTextOnly:@"密码长度为6-16位" withView:self.view];
        return;
    }
    
    if ([account isMobile]) {
        MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:self.navigationController.view];
        [[WCNetworkAccessManager sharedMg] POST:@"nurse/account/resetpwd"
                                     parameters:@{@"mobileNo": account, @"verifyCode":code, @"newPassword":[password md5String]}
                                        success:^(NSDictionary *data){
                                            [[WCPopupLayerManager shareMg] showTextOnly:data[@"data"] withView:self.view];
                                            [self.navigationController popViewControllerAnimated:YES];
                                        } failure:nil netError:nil always:^{
                                            [hud hide:YES];
                                        }];
    } else {
        [[WCPopupLayerManager shareMg] showTextOnly:@"手机号错误" withView:self.view];
        return;
    }
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    UIView *mustShow;
    if (textField.tag == 0) {
        mustShow = self.codeTextField;
    } else if (textField.tag == 1) {
        mustShow = self.pwdTextField;
    } else if (textField.tag == 2) {
        return YES;
    }
    
    CGRect rect = [mustShow convertRect:mustShow.bounds toView:self.view];
    CGFloat y = self.view.frame.size.height - rect.origin.y - rect.size.height - kKeyboardHeight;
    
    #warning 为了配合键盘弹出的动画，使用7<<16.
    [UIView animateWithDuration:0.25
                          delay:0.0
                        options:7<<16
                     animations:^{
                         self.view.transform = CGAffineTransformMakeTranslation(0, y>=0?0:y-10);
                     } completion:NULL];
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.tag == 2) {
        [textField resignFirstResponder];
        [self doSubmit:nil];
        return YES;
    }
    
    return NO;
}

@end
